package com.example.spring.service;

import com.example.spring.entity.Good;
import com.example.spring.entity.Record;
import com.example.spring.mapper.GoodMapper;
import com.example.spring.mapper.RecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;

@Service
public class GoodService {
    @Autowired
    private GoodMapper goodMapper;
    @Autowired
    private RecordMapper recordMapper;

    public int save(Good good){
        if(good.getGid() == null) { //无id新增id
            return goodMapper.insert_data(good);
        }
        else {//否则更新
            return goodMapper.update(good);
        }
    }
    //增加记录
    public void readSlot(Integer gid, Integer sid, Integer whid)
    {
        Integer oldSid = goodMapper.findSlotId(gid);
        Integer oldWhid = goodMapper.findWhid(oldSid);
        Record record1 = new Record();
        Record record2 = new Record();
        record1.setGid(gid);
        record1.setSid(oldSid);
        record1.setWhid(oldWhid);
        record1.setEvent("out");
        record2.setGid(gid);
        record2.setSid(sid);
        record2.setWhid(whid);
        record2.setEvent("in");
        Good good = new Good();
        good.setGid(gid);
        good.setSid(sid);
        if (oldSid != sid) {
            recordMapper.insert_data(record1);
            recordMapper.insert_data(record2);
            goodMapper.update(good);
        }
    }

    //增加记录
    public void readSlot1(Integer gid, Integer sid, Integer whid, Timestamp time)
    {
        Integer oldSid = goodMapper.findSlotId(gid);
        Integer oldWhid = goodMapper.findWhid(oldSid);
        Record record1 = new Record();
        Record record2 = new Record();
        record1.setGid(gid);
        record1.setSid(oldSid);
        record1.setWhid(oldWhid);
        record1.setEvent("out");
        record1.setCreateTime(time);
        record2.setGid(gid);
        record2.setSid(sid);
        record2.setWhid(whid);
        record2.setEvent("in");
        record2.setCreateTime(time);
        Good good = new Good();
        good.setGid(gid);
        good.setSid(sid);
        if (oldSid != sid) {
            recordMapper.insert_data(record1);
            recordMapper.insert_data(record2);
            goodMapper.update(good);
        }
    }

}
